package com.sinoma.G2project.config.datasource;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.sinoma.G2project.config.datasources;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;

@Configuration
public class DataSourceConfig {

    @Autowired
    Environment environment;

    @Bean("dataSources")
    @ConfigurationProperties(prefix = "datasource")
    public datasources datasource(){
        return new datasources();
    }

    @Bean("dynamicDataSource")
    public DynamicDataSource dynamicDataSource(@Qualifier("dataSources") datasources dataSources){
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        HashMap<Object, Object> map = new HashMap<>();
        Class<?> clazz = dataSources.getClass();
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field field : declaredFields) {
            String fieldName = field.getName();
            try {
                Method method = dataSources.getClass().getMethod("get" + fieldName);
                Object invoke = method.invoke(dataSources);
                map.put(fieldName,invoke);
            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        dynamicDataSource.setTargetDataSources(map);
        dynamicDataSource.setDefaultTargetDataSource(map.get("Member"));
        return dynamicDataSource;
    }

    /**
     * 这个SqlSessionFactory可以使用Mybatis默认的
     * @param dataSource
     * @return
     * @throws IOException
     */
    @Bean
    public MybatisSqlSessionFactoryBean setSqlSessionFactory(@Qualifier("dynamicDataSource") DynamicDataSource dataSource) throws IOException {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        // 设置数据源
        bean.setDataSource(dataSource);
        // 简化PO的引用
//        bean.setTypeAliasesPackage("***.****.*****");//这里我打码了，写你项目PO的包
        // 设置全局配置
//        bean.setGlobalConfig(this.globalConfig());
//        bean.setPlugins(pageInterceptor);
        bean.setTypeAliasesPackage(environment.getProperty("mybatis.type-aliases-package"));
        bean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(environment.getProperty("mybatisplus.mapper-locations")));
        return bean;
    }
}
